var express = require('express');  //expres插件
var path = require('path');  //路径插件
var favicon = require('serve-favicon');  //tab页icon插件
var logger = require('morgan');   //日志插件
var cookieParser = require('cookie-parser');  //cookie插件
var bodyParser = require('body-parser');//http 请求体插件
var session = require('express-session');  //session插件
var Settings = require('./database/settings');
var MongoStore = require('connect-mongo')(session);
var db = require('./database/msession');

var routes = require('./routes/index');
var users = require('./routes/users');
var admin = require('./routes/admin'); //后台管理员
var manager = require('./routes/manager');
var comments = require('./routes/comments');
var originMen = require('./routes/originMen');
var lawyer = require('./routes/lawyer');
var account = require('./routes/account');
var thirdCredential = require('./routes/thirdCredential');
var api = require('./routes/api');
var upload = require('./routes/upload');
var download = require('./routes/download');
var multerRouter = require('./routes/multer');
var app = express(); 

app.set('views', path.join(__dirname, 'views'));//设置视图此为path中间件的语法，需要渲染视图时，传递title参数

app.engine('html',require('ejs').renderFile);
app.set('view engine','html')


// app.use(fileUpload());
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(session({
    resave:false,
    saveUninitialized:false,
    cookie: { maxAge: 60000000 },
    secret: 'nrd',
    store: new MongoStore({
       url: 'mongodb://localhost/testccer',
       db:"testccer" 
    })
}));

app.use(function(req, res, next){
    res.locals.user = req.session.user;  //接入session数据保持到本地变量
    next();
 });


app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/login',routes);
app.use('/home',routes);
app.use('/admin',admin);
app.use('/manager',manager);
app.use('/originMen',originMen);
app.use('/account',account);
app.use('/thirdCredential',thirdCredential);
app.use('/comments',comments);
app.use('/lawyer',lawyer);
app.use('/logout',routes);
app.use('/api',api);
app.use('/upload',upload);
app.use('/download',download);
app.use('/multerRouter',multerRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) { //没有指定路径，所有路径都会执行该函数
  res.locals.user = req.session.user;   //请求session.user会被保存到本地变量
  
  var err = req.session.error;    //如果报错删除错误
  delete req.session.error;
  res.locals.message = '';
  if (err) {
     res.locals.message = '<div class="alert alert-warning">' //输出字符串
     + err + '</div>';
     }
  next(); //执行下一个函数
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;
